﻿using System;
using System.Linq;
using Missbot.Service;
using Dolany.UtilityTool;
using MongoDB.Bson.Serialization.Attributes;

namespace Missbot.AIModule.Gaming.SignIn
{
    public class SignInSuccessiveRecord : DbBaseEntity
    {
        public string UserID { get; init; }

        public string GroupID { get; init; }

        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime StartDate { get; set; }

        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime EndDate { get; set; }
    }

    public static class SignInSuccessiveDA
    {
        public static int SuccessiveDays(this SignInSuccessiveRecord entity)
        {
            return (int) (entity.EndDate - entity.StartDate).TotalDays + 1;
        }

        public static bool IsYesterdaySigned(this SignInSuccessiveRecord entity)
        {
            return entity.EndDate.Date == DateTime.Today.AddDays(-1);
        }

        private static SignInSuccessiveRecord GetLastest(string GroupID, string UserID)
        {
            return MongoService<SignInSuccessiveRecord>.Get(p => p.GroupID == GroupID && p.UserID == UserID, p => p.EndDate, false, limit: 1)
                                                       .FirstOrDefault();
        }

        public static bool IsTodaySigned(string GroupID, string UserID)
        {
            return GetLastest(GroupID, UserID)?.EndDate == DateTime.Today;
        }

        public static int TodaySignedGroupCount(string UserID)
        {
            return MongoService<SignInSuccessiveRecord>.Get(p => p.UserID == UserID && p.EndDate == DateTime.Today)
                                                       .Select(p => p.GroupID)
                                                       .Distinct()
                                                       .Count();
        }

        public static SignInSuccessiveRecord Sign(string GroupID, string UserID)
        {
            var lastest = GetLastest(GroupID, UserID);
            if (lastest == null || !lastest.IsYesterdaySigned())
            {
                var newSign = new SignInSuccessiveRecord {UserID = UserID, GroupID = GroupID, StartDate = DateTime.Today, EndDate = DateTime.Today};
                MongoService<SignInSuccessiveRecord>.Insert(newSign);
                return newSign;
            }

            lastest.EndDate = DateTime.Today;
            lastest.Update();
            return lastest;
        }

        public static void Update(this SignInSuccessiveRecord entity)
        {
            MongoService<SignInSuccessiveRecord>.Update(entity);
        }

        public static void Delete(this SignInSuccessiveRecord entity)
        {
            MongoService<SignInSuccessiveRecord>.Delete(entity);
        }

        public static SignInSuccessiveRecord MakeUp(string GroupID, string UserID)
        {
            var lastTwo = MongoService<SignInSuccessiveRecord>.Get(p => p.GroupID == GroupID && p.UserID == UserID, p => p.EndDate, false, limit: 2);
            if (lastTwo.IsNullOrEmpty())
            {
                return null;
            }

            if (lastTwo.Count == 1 || lastTwo[0].StartDate > lastTwo[1].EndDate.AddDays(1))
            {
                lastTwo[0].StartDate = lastTwo[0].StartDate.AddDays(-1);
                lastTwo[0].Update();
                return lastTwo[0];
            }

            lastTwo[0].StartDate = lastTwo[1].StartDate;
            lastTwo[0].Update();
            lastTwo[1].Delete();

            return lastTwo[0];
        }
    }
}